package com.funtl.framework.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 登录超时拦截器
 * @author wmli
 *
 */
public class AjaxTimeoutInterceptor extends AbstractInterceptor {
	private static final long serialVersionUID = -4172186430376053445L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(StrutsStatics.HTTP_RESPONSE);
		if (request.getSession().getAttribute("admin") == null && !request.getRequestURI().endsWith("login") && !request.getRequestURI().endsWith("loginOut")) {
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print("{\"message\":\"请求超时，请重新登录！\"}");
			return null;
		}
		return invocation.invoke();
	}

}
